﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace SpaceMan.Entities
{
    public class MAN_EntityManager
    {
        public List<I_Entity> EntityList;
        List<I_Entity> entitiesToAdd;
        public List<I_Enemy> EnemyList;
        C_Player Player;
        public MAN_EntityManager()
        {
            EnemyList = new List<I_Enemy>();
            entitiesToAdd = new List<I_Entity>();
            EntityList = new List<I_Entity>();
            AddEntity("C_Player", new Vector2(50, 50));
        }

        public void Update()
        {
            foreach(I_Entity Entity in entitiesToAdd)
            {
                assignToReferences(Entity);
            }
            entitiesToAdd.Clear();

            if (Player != null)
            {
                foreach (I_Enemy Enemy in EnemyList)
                {
                    Enemy.GiveParameters(Player.TargetPosition);
                }

                if (Player.MarkedForDeletion)
                {
                    Player.DestroyMe();
                    Player = null;
                }
            }

            for (int i = EntityList.Count - 1; i >= 0; i--)
            {
                EntityList[i].Update();
                if (EntityList[i].MarkedForDeletion)
                {
                    EntityList[i].DestroyMe();
                    EntityList[i] = null;
                    EntityList.RemoveAt(i);
                }
                
            }

            for (int i = EnemyList.Count - 1; i >= 0; i--)
            {
                if (EnemyList[i].MarkedForDeletion)
                {
                    EnemyList.RemoveAt(i);
                }
            }
        }

        

        public void Draw(SpriteBatch spriteBatch, DrawMode mode,Rectangle DrawSpace)
        {
            foreach (I_Entity entity in EntityList)
            {
                entity.Draw(spriteBatch, mode, DrawSpace);
            }
        }


        public void DestroyAll()
        {
            EntityList.Clear();
        }

        public void AddEntity(string EntityName, Vector2 Position)
        {
            string ns = "SpaceMan.Entities";
            Type type = Type.GetType(ns + "." + EntityName);

            if (type != null)
            {
                if (!type.IsInstanceOfType(typeof(I_Projectile)))
                {
                    I_Entity Ent;
                    Ent = (I_Entity)type.GetConstructor(new Type[] { }).Invoke(new object[] { });
                    Ent.Initialize(Position);
                    entitiesToAdd.Add(Ent);
                }

                
            }
        }

        void assignToReferences(I_Entity Entity)
        {
            if (Entity is I_Enemy)
            {
                //Entity.Initialize(Position);
                EnemyList.Add((I_Enemy)Entity);
                EntityList.Add(Entity);
            }

            if (Entity is C_Player)
            {
                this.Player = (C_Player)Entity;
                EntityList.Add(Player);
            }

            else
            {

                EntityList.Add(Entity);
            }
        }

        public void giveEntity(I_Entity entity)
        {

            entitiesToAdd.Add(entity);
        }
    }
}
